
/**
 * 
 * @class ThreadedObserverSubject ThreadedObserver
 *
 * The ThreadedObserver Pattern for threaded classes
 *
 * $Id: ThreadedObserver.cpp 2 2012-05-22 17:55:16Z gerry@gaboury.biz $
 */


#include "ThreadedObserver.hpp"

ThreadedObserver::ThreadedObserver ( ThreadedObserverSubject* p_subject ) :
  m_subject(p_subject)
{ }

ThreadedObserver::~ThreadedObserver ()
{
  //if ( m_subject )
  //  m_subject->detach(this);
}

void
ThreadedObserver::detach()
{
  m_subject = 0;
}

ThreadedObserverSubject::~ThreadedObserverSubject()
{ }

void
ThreadedObserverSubject::attach ( ThreadedObserver* p_observer )
{
  // the return value could be checked
  m_observers.insert ( p_observer );
  // set subject
  //p_observer->subject ( this );
}

void
ThreadedObserverSubject::detach ( ThreadedObserver*  p_observer )
{
  ThreadedObserverSetIter iter;
  if ( ( iter = m_observers.find ( p_observer ) ) != m_observers.end() )
    {
      m_observers.erase ( iter );
    }
  // unset subject
  //p_observer->subject(0);
}

void
ThreadedObserverSubject::notify (int ith)
{
  //cerr << "thread " << ith << " notify - start" << endl;
  ThreadedObserverSetIter iter;
  for ( iter = m_observers.begin(); iter != m_observers.end(); ++iter )
    {
      (*iter)->update(ith);
    }
  //cerr << "thread " << ith << " notify - end" << endl;
}
